import router from './router'
import nprogress from 'nprogress'
import pinia from './store'
import useUserStore from './store/modules/UserStore'
import { message } from 'ant-design-vue'
const userStore = useUserStore(pinia)
const blacklist: string[] = ['template', 'editor']
router.beforeEach((to, from, next) => {
  nprogress.start()

  if (to.path === '/login') {
    if (userStore.userInfo.token) {
      next('/')
    } else {
      next()
    }
  } else {
    if (userStore.userInfo.token) {
      next()
    } else {
      if (blacklist.includes(to.name as string)) {
        next('/login')
        message.error('请先登录')
      } else {
        next()
      }
    }
  }
})

router.afterEach(() => {
  nprogress.done()
})
